/*
   Search for "DEBUG" to find stuff that shouldn't be in the release version.

	Last Edited By: Michael Greene
 */

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h> //required for toupper
#include <string.h>

#include "handGrading.h"
#include "poker.h"
#include "utils.h"


#define YESRND 			//change to NORND for not random numbers, YESRND makes true random
#define UNIX		    //change to Unix for *nix and Dos for windows
#include "switches.h" 	// must come after the switches are set


// global variables
char * faces[] = {"", "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
char * colors[]= {"RED", "BLACK"};
CARDCHARS;

int bet;
int balance;
int creditsEntered;
int sound = 0;


int main(int argc, char *argv[])
{
	CARD deck[52];
	CARD hand[5];
	int i = 0;
	int creditsBet;

	//sound or not, or --help, or /?
	if (argc > 1) {
		if ( !strcmp(argv[1], "--sound") ) {
			sound = 1;
		} else {
			printf("Usage: %s [--sound]\n\n  --sound\tactivates optional sounds. Requires SoX\n\n", argv[0]);
			exit(0);
		}
	}

	playSound(ENTRY);

	genDeck(deck);

	balance = 500;	//Initial credit balance

	//for(i=0;i<52;i++)printf("\n%s of %c", faces[deck[i].face], suits[deck[i].suit]); //uncomment to print the deck

	while (balance > 0) //Keep playing as long as we have positive credits!
	{
		shufDeck(deck);
		for(i=0;i<52;i++)
		{
			deck[i].dealt = 0;
		}

		do //get bet
		{
         printf("You Have %d Credits.\n", balance);
			creditsBet = getInt("Insert Credits, 1-5 (or 0 to quit): ");
		} while(creditsBet < 0 || creditsBet > 5);
		balance -= creditsBet;


		//exit condition
		if (creditsBet == 0) {
			//print the heading
			for(i=0;i<79;i++) { printf("-"); }
			printf("\nBalance: %d             <<<POKER>>>\n", balance);
			for(i=0;i<79;i++) { printf("-"); }
			printf("\n");
			printf("    Thanks for playing!\n\n");
			break;
		}

		//print the heading
		for(i=0;i<79;i++) { printf("-"); }
		printf("\nBalance: %d             <<<POKER>>>             Bet:%d\n", balance, creditsBet);
		for(i=0;i<79;i++) { printf("-"); }

		printf("\n\n");

		dealHand(hand, deck);
		printHand(hand);
		draw(hand, deck);
		printHand(hand);

		balance += winnings(score(hand), creditsBet);

		pause(); //Pause

		clearScreen();
	}

	if (balance <= 0) {
		printf("You are out of credits. Sorry!\n\n");
	}

	playSound(EXIT);

	return(0);
}

////////////////////////////////////////////////////////////////

//creates a 52 deck card into the passed in deck pointer
void genDeck(CARD aDeck[])
{
	int i;
	int suit=HEART;
	//generates the deck;
	for(i=0; i<52; i++)
	{
		aDeck[i].face = (i%13) +1;
		aDeck[i].suit = suit;
		aDeck[i].kept = 0; //initialize to 0
		aDeck[i].dealt = 0; //set to 0 because no cards have been dealt
		if((i+1)%13==0 && i != 0) suit++;
	}
}

////////////////////////////////////////////////////////////////

//sort a deck of 52 cards
void shufDeck(CARD aDeck[])
{
	int i;
	short moveFrom;
	short moveTo;
	CARD temp;

	//reset deals
	for(i = 0; i<52; i++) {
		aDeck[i].dealt = 0;
	}

	INITRAND;

	for(i=0;i<SHUFFLES;i++)
	{
		moveFrom = rand() % 52;
		moveTo = rand() % 52;
		if(moveTo != moveFrom)
		{
			temp = aDeck[moveTo];
			aDeck[moveTo] = aDeck[moveFrom];
			aDeck[moveFrom] = temp;
		}
	}

}

////////////////////////////////////////////////////////////////

//deals a hand of 5 cards to the passed in hand pointer from the passed in deck pointer
void dealHand(CARD aHand[], CARD aDeck[])
{
	int i;
	for(i=0;i<5;i++)
	{
		aHand[i] = aDeck[i];
		aDeck[i].dealt = 1;
	}
}

////////////////////////////////////////////////////////////////

void printHand(CARD aHand[])
{
	int i;
	int x, y;

	printf("%c", L_T_CORN);
	for(y=0; y<5; y++) {
		for(x = 0; x<8; x++) { printf("%c", HORIZ); }
		if(y<4) { printf("%c", T_TEE); }
	}
	printf("%c\n", R_T_CORN);

	printf("%c   1    %c   2    %c   3    %c   4    %c   5    %c\n", VERT, VERT, VERT, VERT, VERT, VERT);

	printf("%c", L_TEE);
	for(y=0; y<5; y++) {
		for(x = 0; x<8; x++) { printf("%c", HORIZ); }
		if(y<4) { printf("%c", CROSS); }
	}
	printf("%c\n", R_TEE);

	for(i=0;i<5;i++)printf("%c %c%2s    ", VERT, suits[aHand[i].suit], faces[aHand[i].face]);
	printf("%c\n", VERT);
	printf("%c        %c        %c        %c        %c        %c\n", VERT, VERT, VERT, VERT, VERT, VERT);
	printf("%c        %c        %c        %c        %c        %c\n", VERT, VERT, VERT, VERT, VERT, VERT);
	printf("%c        %c        %c        %c        %c        %c\n", VERT, VERT, VERT, VERT, VERT, VERT);
	printf("%c        %c        %c        %c        %c        %c\n", VERT, VERT, VERT, VERT, VERT, VERT);
	for(i=0;i<5;i++){ printf("%c    %-2s%c ", VERT, faces[aHand[i].face], suits[aHand[i].suit]); }
	printf("%c\n", VERT);

	printf("%c", L_B_CORN);
	for(y=0; y<5; y++) {
		for(x = 0; x<8; x++) { printf("%c", HORIZ); }
		if(y<4) { printf("%c", B_TEE); }
	}
	printf("%c\n", R_B_CORN);

	printf("\n");
}

////////////////////////////////////////////////////////////////

void draw(CARD aHand[], CARD aDeck[])
{
	int i;
	int x = 0;
	int eegg = 0;
	char ans = 'y';

	for(i=0; i<5; i++)
	{
		printf("%*s keep %d?", (9*i), "", i+1);

		ans = getOneChar();
		ans = toupper(ans);

		if(ans == 'N')
		{
			while(aDeck[x].dealt) { x++; } //advance to card that has not been dealt
			aHand[i] = aDeck[x];
			aDeck[x].dealt = 1;
		}

		//easter egg!
		if( (i == 0) && ans == 'N') { eegg++; }
		if( (i == 1) && ans == 'I') { eegg++; }
		if( (i == 2) && ans == 'K') { eegg++; }
		if( (i == 3) && ans == 'T') { eegg++; }
		if( (i == 4) && ans == 'O') { eegg++; }
 	}

	//easter egg
	if (eegg == 5) {
		//printf("EASTER EGG!\n"); //uncomment for DEBUG
		playSound(EASTER_EGG);

		shufDeck(aDeck);
		aHand[0].face = 1;
		aHand[1].face = 13;
		aHand[2].face = 12;
		aHand[3].face = 11;
		aHand[4].face = 10;
		for(x = 0; x < 5; x++) {
			aHand[x].suit = 0;
		}
	}
}

void playSound(int event) {
	char *sounds[6][15] = {
			//ENTRY
			{ "fox-rnr.wav", "hello1_x.wav", "dickfur.wav", "", "", 
            "", "", "", "", "", 
            "", "", "", "", "",},
			//EXIT
			{ "fal-gameover.wav", "pig-cantbe.wav", "wouldnt_do_that.wav", "bill_ted_youlose.wav", "make_like_tree.wav", 
            "still_here1.wav", "flintstonefired.wav", "", "", "", 
            "", "", "", "", "", },
			//WIN
			{ "falktail.wav", "fal-yeah.wav", "fal-admitgood.wav", "wolfgood.wav", "wide_wide_world.wav", 
            "bill_ted_excellent.wav", "clerks_not_be_here.wav", "gnarly.wav", "have_a_pepsi.wav", "tent.wav", 
            "hail2king.wav", "boomstick.wav", "givemesugar.wav", "", "", },
			//LOSE
			{ "fox-ahhh.wav", "fal-wrong-gdiff.wav", "falbuddy.wav", "slip-takethat.wav", "shogun-eeeeyyaaaagh.wav", 
            "depressed.wav", "bill_ted_bogus.wav", "ghostbusters_bad.wav", "inconceivable.wav", "hook_up.wav", 
            "", "", "", "", "", },
			//BIGWIN
			{ "peproll1.wav", "leonded1.wav", "great_scott.wav", "", "", 
            "", "", "", "", "", 
            "", "", "", "", "", },
			//EASTER_EGG
			{ "fal-jeezlaweez.wav", "wolfheck.wav", "", "", "", 
            "", "", "", "", "", 
            "", "", "", "", "", }
	};
	int maxsubs[6] = {
			3,  //ENTRY
            7,  //EXIT
            12,  //WIN
            10,  //LOSE
            3,  //BIGWIN
            2}; //EASTER_EGG
    
	int playNum = 0;
	char cbuff[50];

	INITRAND;

	if (sound) {
        if (maxsubs[event]) {
		       playNum = rand() % maxsubs[event];
        }
		sprintf(cbuff, "%s Sounds%c%s -d %s",PLAYFILECMD, DIRSEP, (sounds[event][playNum]), HIDE);
		//printf("PLAY COMMAND OUTPUT [ %s ]\n", cbuff); //Uncomment for DEBUG
		system(cbuff);
	}
}
